import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/style/image_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

import 'default_bottom_sheet.dart';

/// 照片预览
showPhotoPreviewBottomSheet(
  BuildContext context,
  File file,
  OcrAttachmentEntity attachment, {
  VoidCallback onSelect,
  VoidCallback onChooseAgain,
  VoidCallback onAttach,
  String attachName,
  VoidCallback onChoosePhotoLibrary,
  VoidCallback onCrop,
  VoidCallback onRemove,
  VoidCallback onRetakePhoto,
  bool canDelete: false,
  String chooseAgainText,
}) =>
    showDefaultBottomSheet(context,
        child: _PhotoPreview(
          onChooseAgain: onChooseAgain,
          onSelect: onSelect,
          onChoosePhotoLibrary: onChoosePhotoLibrary,
          onRetakePhoto: onRetakePhoto,
          onAttach: onAttach,
          file: file,
          attachment: attachment,
          attachName: attachName,
          onCrop: onCrop,
          onRemove: onRemove,
          chooseAgainText: chooseAgainText,
        ),
        isScrollControlled: true,
        leading: canDelete
            ? DefaultBottomSheetLeadingButton(
                title: S.current.delete,
                onTap: () => Navigator.of(context).pop(true),
              )
            : null,
        title: S.current.preview);

class _PhotoPreview extends StatelessWidget {
  final VoidCallback onSelect;
  final VoidCallback onChooseAgain;
  final File file;
  final VoidCallback onAttach;
  final String attachName;

  final VoidCallback onChoosePhotoLibrary;
  final VoidCallback onCrop;
  final OcrAttachmentEntity attachment;
  final VoidCallback onRetakePhoto;
  final VoidCallback onRemove;
  final String chooseAgainText;

  const _PhotoPreview(
      {Key key,
      this.attachment,
      this.onSelect,
      this.onChooseAgain,
      this.file,
      this.onAttach,
      this.onChoosePhotoLibrary,
      this.onCrop,
      this.onRetakePhoto,
      this.attachName,
      this.onRemove,
      this.chooseAgainText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(height: 30),
        Stack(
          children: [
            AspectRatio(
              aspectRatio: 1,
              child: file == null
                  ? Image(
                      image: Config.toImageProvider(attachment.url),
                      frameBuilder: ImageStyle.fadeInFrame,
                      fit: BoxFit.cover,
                    )
                  : Image.file(
                      file,
                      frameBuilder: ImageStyle.fadeInFrame,
                      fit: BoxFit.cover,
                    ),
            ),
            if (onCrop != null)
              Positioned(
                  top: getDp(14),
                  right: getDp(23),
                  child: PressedOpacityButton(
                    onPressed: onCrop,
                    color: aceDarkGrayColor50,
                    padding: EdgeInsets.symmetric(
                      horizontal: getDp(12),
                      vertical: getDp(11),
                    ),
                    borderRadius: BorderRadius.circular(getDp(10)),
                    child: SvgPicture.asset(
                      A.assetsImagesStyleCrop_2,
                      width: getDp(18),
                      height: getDp(18),
                    ),
                  )),
          ],
        ),
        SizedBox(
          height: getDp(36),
        ),
        if (onSelect != null)
          NormalButton(
            title: S.current.select,
            onTap: onSelect,
          ),
        if (onAttach != null)
          NormalButton(
            title: attachName ?? S.current.attach,
            onTap: onAttach,
          ),
        SizedBox(
          height: getDp(15),
        ),
        if (onRetakePhoto != null)
          NormalButton(
            onTap: onRetakePhoto,
            title: S.current.retake_photo,
            isBorder: true,
          ),
        if (onChooseAgain != null)
          NormalButton(
            onTap: onChooseAgain,
            title: chooseAgainText ?? S.current.choose_another_photo,
            isBorder:
                chooseAgainText == S.current.choose_again && onRemove == null,
          ),
        if (onChoosePhotoLibrary != null)
          NormalButton(
            onTap: onChoosePhotoLibrary,
            title: S.current.choose_photo_library,
            isBorder: true,
          ),
        if (onRemove != null)
          SizedBox(
            height: getDp(15),
          ),
        if (onRemove != null)
          NormalButton(
            onTap: onRemove,
            title: S.current.remove,
            isBorder: true,
          ),
      ],
    );
  }
}
